<template>
  <div class="content-workbench">
    <div class="inner-container">
      <div class="inner-container-header" v-loading="stationStatisticsLoading">
        <div class="toDo">
          <h1>
            <i class="iconfont title-icon">&#xe6e3;</i>
            我的内容
            <div class="more-toDo" @click="goRoute('/content2/my/release', { state: 99 })">
              详情
              <i class="iconfont details-icon">&#xe601;</i>
            </div>
          </h1>
          <div class="myManuscript-box">
            <div class="detail-box title-box allNum center-detail-box">
              <span class="label">我的稿件</span>
              <span class="num blue">{{ totalNum }}</span>
            </div>
            <div class="detail-box">
              <div class="row">
                <div class="detail" @click="goRoute('/content2/my/release', { state: 99 })">
                  <span class="label">已发布：</span>
                  <span class="value">{{ stationStatisticsInfo[99] }}</span>
                </div>
                <div class="detail" @click="goRoute('/content2/my/release', { state: 5 })">
                  <span class="label">待发布：</span>
                  <span class="value">{{ stationStatisticsInfo[5] }}</span>
                </div>
                <div class="detail" @click="goRoute('/content2/my/release', { state: 2 })">
                  <span class="label">撤稿箱：</span>
                  <span class="value">{{ stationStatisticsInfo[2] }}</span>
                </div>
              </div>
            </div>
            <div class="detail-box">
              <div class="row">
                <div class="detail" @click="goRoute('/content2/my/release', { state: 1 })">
                  <span class="label">草稿箱：</span>
                  <span class="value">{{ stationStatisticsInfo[1] }}</span>
                </div>
                <div class="detail" @click="goRoute('/content2/my/collect', { state: 99 })">
                  <span class="label">我的收藏：</span>
                  <span class="value">{{ stationStatisticsInfo[100] }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="toDo">
          <h1>
            <i class="iconfont title-icon">&#xe6db;</i>
            审核
            <div class="more-toDo" @click="goFlow('/flow/task/pending', { moduleId: 1 })">
              详情
              <i class="iconfont details-icon">&#xe601;</i>
            </div>
          </h1>
          <div class="myManuscript-box">
            <div class="detail-box title-box allNum" @click="goFlow('/flow/task/started', { moduleId: 1 })">
              <span class="label">我提交的审核</span>
              <span class="num blue">{{ stationStatisticsInfo.myFlowing }}</span>
            </div>
            <div class="detail-box title-box allNum" @click="goFlow('/flow/task/pending', { moduleId: 1 })">
              <span class="label">待我审核</span>
              <span class="num">{{ stationStatisticsInfo.myAudit }}</span>
            </div>
            <div class="detail-box title-box allNum" @click="goRoute('/content2/my/release', { state: 3 })">
              <span class="label">待处理退稿</span>
              <span class="num">{{ stationStatisticsInfo.myReject }}</span>
            </div>
          </div>
        </div>
      </div>
      <!-- 快速发布 -->
      <el-card class="release-card" v-loading="navListLoading">
        <template #header>
          <div class="release-header">
            <span class="release-title">快速发布</span>
            <span class="release-more" @click="linkMore">更多发布</span>
          </div>
        </template>
        <ul>
          <template v-for="(item, index) in navList" :key="index">
            <li v-if="item.showModel">
              <a href="javascript:;" @click="linkTo(item)">
                <div class="icon-box">
                  <i :class="item.icon + ' iconfont'" :style="{ color: themeColor }"></i>
                </div>
                <p>{{ item.name }}</p>
              </a>
            </li>
          </template>
        </ul>
      </el-card>
      <!-- 最近发布 -->
      <el-card class="release-card" v-loading="navListLoading">
        <template #header>
          <div class="release-header">
            <span class="release-title">最近发布</span>
          </div>
        </template>
        <RuifoxTable :loading="tableLoading" :default-index="true" :default-checkbox="false" v-model:tableData="tableData" v-model:fieldList="fieldList">
          <!-- 使用情况 -->
          <template #publish_time="{ element }">
            <span>{{ dayjs(element.publish_time).format('YYYY-MM-DD HH:mm:ss') }}</span>
          </template>
          <!-- 来源 -->
          <template #comefrom="{ element }">
            <span>{{ element.comefrom || '-' }}</span>
          </template>
          <template #create_time="{ element }">
            <span>{{ dayjs(element.create_time).format('YYYY-MM-DD HH:mm:ss') }}</span>
          </template>
          <!-- 操作列 -->
          <!-- <template #options="{ element }">
            <div class="button-box">
              <el-button v-permission="['build:module:column:edit']" link type="primary" @click="handlePreview(element)">预览</el-button>
            </div>
          </template> -->
        </RuifoxTable>
      </el-card>
    </div>
    <QuickPublish v-if="showChooseCategory" v-model:showDialog="showChooseCategory" :pushCategoryList="categoryList"
      :pushCategoryCommonList="commonCategoryList" :isSingle="true" :curModel="curModel">
    </QuickPublish>
    <!-- 更多发布 -->
    <el-dialog v-model="quickPublishMoreOpen" class="quick-publish-dialog" title="快速发布" width="30%" align-center>
      <template #header>
        <el-tabs v-model="quickPublishActive" class="quick-publish-dialog-tabs">
          <el-tab-pane label="发布模型" :name="0"></el-tab-pane>
        </el-tabs>
      </template>
      <div class="des">
        <div class="left">类型</div>
        <div class="right">显示</div>
      </div>
      <!-- 发布模型 -->
      <ul class="more-publish" v-if="quickPublishActive === 0">
        <li v-for="(item, index) in navList" :key="index">
          <a href="javascript:;" @click="linkTo(item)">
            <Icon :name="item.icon" />
            <p>{{ '发布' + item.name }}</p>
          </a>
          <el-switch v-model="item.showModel" :active-value="1" :inactive-value="0" @change="
            val => {
              return handleModelShowChange(item)
            }
          " />
        </li>
      </ul>
    </el-dialog>
  </div>
</template>

<script setup>
import { reactive, ref, nextTick, computed } from 'vue'
import { selectAllModel } from '@/api/model'
import { getCategoryByModelId } from '@/api/category'
import { getContentStatistics, selectRecentContent } from '@/api/referenceData'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { setLocCanReleaseModel, getLocCanReleaseModel } from '@/utils/storage'
import { jumpRoute, hasPermission } from '@ruifox-cms/utils'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import QuickPublish from './components/QuickPublish.vue'


const store = useStore()
const router = useRouter()

const siteId = computed(() => store.state.site.currentSite?.id)
const themeColor = store.state.base.themeColor

const stationStatisticsInfo = ref({})
const stationStatisticsLoading = ref(false)
const totalNum = computed(() => {
  let result = 0, data = stationStatisticsInfo.value
  result = data[99] + data[0] + data[1] + data[2] + data[3] + data[4] + data[5]
  return result
})
// 获取工作台顶部统计（我的内容+审核）
const handleGetStationStatistics = async () => {
  stationStatisticsLoading.value = true
  let res = await getContentStatistics({ catId: -1, selectAll: true })
  stationStatisticsInfo.value = res.data
  stationStatisticsLoading.value = false
}
handleGetStationStatistics()

// 跳转待审详情(moduleId: 1标识站群模块的审核流)
const goFlow = (path, query) => {
  jumpRoute(path, query, '_blank')
}

const handleModelShowChange = val => {
  if (navList.value.filter(item => item.showModel == 1).length < 1) {
    nextTick(() => {
      val.showModel = 1
      ElMessage({
        message: '至少保留一项',
        type: 'warning'
      })
    })
  }
  let list = navList.value.map(item => {
    return {
      id: item.id,
      showModel: item.showModel === undefined ? 1 : item.showModel
    }
  })
  // 本地缓存显隐状态
  setLocCanReleaseModel({
    navStatusList: list
  })
}
const navList = ref([])
const navStatusList = ref([]) //navList显示隐藏状态列表
const navListLoading = ref(true)
const handleGetCanReleaseModel = () => {
  let locData = getLocCanReleaseModel()
  navStatusList.value = locData?.navStatusList.length ? locData.navStatusList : []
  // 获取可以发布模型列表
  selectAllModel({ state: 1 }).then(res => {
    // 获取本地显隐状态
    navList.value = res.data.map(item => {
      let curObj = navStatusList.value.find(tmp => tmp.id === item.id && item.state === 0)
      return {
        ...item,
        showModel: curObj === undefined ? 1 : curObj.showModel
      }
    })
    navListLoading.value = false
    handleModelShowChange()
  })
}
handleGetCanReleaseModel()
const quickPublishActive = ref(0)

const curModel = ref(null)
const pushCategory = ref('')
const showChooseCategory = ref(false)
const goModel = reactive({})
const categoryList = ref([])
const commonCategoryList = ref([]) // java后台暂无最近常用栏目的接口
const linkTo = item => {
  if (!hasPermission(['content:workStation:publish'])) {
    ElMessage.warning('您没有发布权限，请联系管理员添加')
    return
  }
  curModel.value = item
  showChooseCategory.value = true
  pushCategory.value = ''
  Object.assign(goModel, item)
  getCategoryByModelId({ modelId: item.id, siteId: siteId.value }).then(res => {
    categoryList.value = res.data
  })
}
const quickPublishMoreOpen = ref(false)
const linkMore = () => {
  quickPublishMoreOpen.value = true
}
const goRoute = (path, query) => {
  if (path) router.push({ path, query })
}

// 最近发布
const fieldList = ref([
  {
    title: '标题',
    name: 'title'
  },
  {
    title: '来源',
    name: 'comefrom'
  },
  {
    title: '创建时间',
    name: 'create_time'
  },
  {
    title: '发布时间',
    name: 'publish_time',
    width: 160
  }
])
const tableLoading = ref(false)
const tableData = ref([])
const getRecentContent = async () => {
  tableLoading.value = true
  const res = await selectRecentContent()
  if (res.code === 20000) {
    tableData.value = res.data
  }
  tableLoading.value = false
}
getRecentContent()
</script>

<style scoped lang="scss">
.content-workbench {
  flex: 1;

  .inner-container {
    width: 100%;

    .inner-container-header {
      display: flex;
      flex-wrap: wrap;
      gap: 16px;
      margin-bottom: 16px;
    }

    .toDo {
      width: calc((100% - 32px) / 3);
      min-width: 380px;
      height: 170px;
      background-color: #fff;
      border-radius: 4px;
      box-shadow: 0px 0px 12px rgba(0, 0, 0, 0.12);
      box-sizing: border-box;
      padding: 16px;
      display: flex;
      flex-direction: column;

      h1 {
        color: #333;
        font-size: 17px;
        margin-bottom: 16px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        position: relative;

        .more-toDo {
          position: absolute;
          top: 50%;
          right: 0;
          transform: translateY(-50%);
          font-size: 14px;
          color: #666;
          font-weight: normal;
          cursor: pointer;

          .details-icon {
            transform: rotate(-90deg);
            font-size: 14px;
            margin-left: 2px;
          }
        }

        .title-icon {
          font-size: 20px;
          margin-right: 8px;
        }
      }

      .myManuscript-box {
        width: 100%;
        height: 90px;
        display: flex;
        justify-content: center;
        align-items: center;

        .allNum {
          span {
            white-space: nowrap;
            font-size: 15px;
            color: #666;
          }

          .num {
            font-size: 32px;
            color: #333;
            margin-top: 12px;
            font-weight: 600;
          }

          .blue {
            color: $mainColor;
          }
        }

        .detail-box {
          width: 130px;
          height: 100%;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          align-items: center;
          margin: 0 10px;

          .row {
            display: flex;
            flex-direction: column;
            align-items: center;
            height: 100%;
            width: 100%;

            .detail {
              color: #555;
              font-size: 15px;
              display: flex;
              justify-content: flex-start;
              align-items: center;
              flex-wrap: nowrap;
              width: 100%;
              height: 33.333%;
              cursor: pointer;

              .label {
                color: #666;
              }

              .value {
                color: #333;
                font-weight: 500;
              }

              span {
                white-space: nowrap;
              }

              &:hover {
                span {
                  @include textHover;
                }
              }
            }
          }
        }

        .title-box {
          justify-content: center;
          align-items: flex-start;
          cursor: pointer;
        }

        .center-detail-box {
          position: relative;

          &::before {
            content: '';
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 1px;
            height: 100%;
            background: #e8e8e8;
          }
        }
      }
    }

    .release-card {
      margin-bottom: 16px;

      .release-header {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .release-title {
          font-size: 17px;
          font-weight: 600;
        }

        .release-more {
          font-size: 14px;
          color: #666;
          font-weight: normal;
          cursor: pointer;
        }
      }

      ul {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-wrap: wrap;
        width: 100%;
        gap: 20px;
        padding: 0;

        li {
          padding: 0;
          margin: 0;

          a {
            height: 100%;
            width: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 12px 16px;

            .icon-box {
              width: 48px;
              height: 48px;
              border-radius: 8px;
              display: flex;
              justify-content: center;
              align-items: center;
              background: $checkColor;
              transition: all 0.2s;
            }

            i {
              font-size: 26px;
            }

            p {
              margin-top: 10px;
              color: #333;
              font-size: 14px;
              font-weight: 500;
            }
          }
        }
      }
    }
  }

  ::v-deep(.quick-publish-dialog) {
    .el-dialog__header {
      border-bottom: none;
      padding: 16px 16px 0;
    }

    .el-dialog__body {
      padding: 16px;
    }

    .des {
      width: 100%;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 0 0 12px;
      color: #666;
      font-size: 14px;
    }
  }

  .more-publish {
    padding-left: 0;

    li {
      font-size: 14px;
      padding: 10px 12px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      cursor: pointer;

      &:hover {
        background: #f5f5f5;

        p {
          color: $mainColor;
        }
      }

      a {
        display: flex;
        justify-content: flex-start;
        align-items: center;

        p {
          margin-left: 8px;
        }

        i {
          font-size: 18px;
        }
      }
    }
  }
}
</style>
